package com.ruoyi.web.controller.resourcepool;


import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.domain.DO.RetrievalSearchParams;
import com.ruoyi.system.domain.SysClasses;
import com.ruoyi.system.domain.SysDataProperty;
import com.ruoyi.system.domain.vo.R;
import com.ruoyi.system.protocol.IDynamicCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/knowledgeBase")
public class KnowledgeBaseController {

    @Autowired
    private IDynamicCacheService dynamicCache;

    /**
     * 首页导航入口
     * @return
     */
    @PostMapping("/getNavigation")
    public R getNavigation(){
        List<SysClasses> classesListByCache = dynamicCache.getClassesListByCache();
        List<SysClasses> navigationList = classesListByCache.stream().filter(x -> "1".equals(x.getIndexNavigation())).collect(Collectors.toList());
        return R.success().data("list", navigationList);
    }

    /**
     * 首页快捷入口
     * @return
     */
    @PostMapping("/getEntrance")
    public R getEntrance(){
        List<SysClasses> classesListByCache = dynamicCache.getClassesListByCache();
        List<SysClasses> entranceList = classesListByCache.stream().filter(x -> "1".equals(x.getIndexEntrance())).collect(Collectors.toList());
        return R.success().data("list", entranceList);
    }

      /**
     * 获取资源库
     * @return
     */
    @PostMapping("/getDb")
    public R getDb(){
        List<SysDictData> dictDb = dynamicCache.getDictDataListByCache("business_db");
        List<SysDictData> dbList = dictDb.stream().filter(x -> "0".equals(x.getStatus())).collect(Collectors.toList());
        return R.success().data("list", dbList);
    }


    /**
     * 获取Web列表页展示字段
     * @return
     */
    @PostMapping("/getListDisplayFields")
    public R getListDisplayFields(@RequestBody RetrievalSearchParams retrievalSearchParams){
        Object classesEnObj = retrievalSearchParams.getClassesEn();
        List<String> classesEnList = new ArrayList<>();
        if(classesEnObj != null && !"".equals(classesEnObj)){
            if(classesEnObj instanceof String){
                classesEnList.add((String) classesEnObj);
            }else if(classesEnObj instanceof List<?>){
                classesEnList = (List<String>) classesEnObj;
            }
        }else{
            List<SysClasses> classesListByCache = dynamicCache.getClassesListByCache();
            classesEnList = classesListByCache.stream().filter(x -> "1".equals(x.getGeneralSearch())).map(x -> x.getNameEn()).collect(Collectors.toList());
        }
        List<SysDataProperty> all = new ArrayList<>();
        for (String classesEn : classesEnList) {
            List<SysDataProperty> dataPropertyListCache = dynamicCache.getDataPropertyListCache(classesEn);
            all.addAll(dataPropertyListCache);
        }
        List<SysDataProperty> webListShow = all.stream().filter(x -> "1".equals(x.getWebListShow())).collect(Collectors.toList());
        webListShow = webListShow.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getNameEn()))),
                        ArrayList::new));
        return R.success().data("list", webListShow);
    }



}
